package cn.hxth.dubbo.service.impl;

import cn.hxth.api.model.Plant;
import cn.hxth.api.query.PageResult;
import cn.hxth.api.query.PlantQueryObject;
import cn.hxth.api.service.IPlantService;
import cn.hxth.api.util.SplitUtil;
import cn.hxth.dubbo.service.mapper.PlantMapper;
import cn.hxth.dubbo.service.mq.RabbitMQSender;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Transactional
@Service
public class PlantServiceImpl implements IPlantService {
    @Resource
    private PlantMapper plantMapper ;
    @Override
    public boolean insertPlant(Plant plant) {
       boolean flag = this.plantMapper.insert(plant) ;
        if(flag){
            this.rabbitMQSender.send("data-change-queue","{\"event_type\": \"add\", \"data_type\": \"plant\", \"cid\": " + plant.getCid() +   "}");
        }
        return  flag ;
    }
    @Resource
    private RabbitMQSender rabbitMQSender;
    @Resource(name = "myRedisTemplate")
    private RedisTemplate<String,Object> redisTemplate ;
    @Override
    public boolean updatePlant(Plant plant) {
        boolean flag = this.plantMapper.updateByPrimaryKey(plant) ;
        if(flag){
            this.rabbitMQSender.send("data-change-queue","{\"event_type\": \"update\", \"data_type\": \"plant\", \"cid\": " + plant.getCid() +   "}");
        }
        return  flag ;
    }

    @Override
    public boolean removePlant(Integer cid) {
        boolean flag = this.plantMapper.deleteByPrimaryKey(cid) ;
        if(flag){
            this.rabbitMQSender.send("data-change-queue","{\"event_type\": \"delete\", \"data_type\": \"plant\", \"cid\": " + cid +   "}");
        }
        return flag ;
    }

    @Override
    public Plant getPlant(Integer cid) {
        String jsonStr = (String) this.redisTemplate.opsForValue().get("plant_"+cid);
        if("".equals(jsonStr)||jsonStr==null){
            Plant plant = this.plantMapper.selectByPrimaryKey(cid);
            this.redisTemplate.opsForValue().set("plant_"+cid,JSONObject.toJSONString(plant));
            return  plant ;
        }

        return JSONObject.parseObject(jsonStr,Plant.class) ;
    }

    @Override
    public Plant getPlantForMq(Integer cid) {
        return    this.plantMapper.selectByPrimaryKey(cid);
    }

    @Override
    public PageResult findAll( PlantQueryObject plantQueryObject) {
        Set<String> keys = null ;
        Integer count = this.plantMapper.selectForCount(plantQueryObject);
        if(plantQueryObject.getSortFlag()==1){
            keys = this.redisTemplate.keys("plant_*") ;}
        if(keys.size()==count){
            List<Object> allData = this.redisTemplate.opsForValue().multiGet(keys) ;
            List<Object> ListData = SplitUtil.splitList(allData, plantQueryObject.getCurrentPage(),plantQueryObject.getPageSize()) ;
            return new PageResult(ListData,allData.size(),plantQueryObject.getCurrentPage(),plantQueryObject.getPageSize()) ;
        }
        else {
            List<Plant> listData = this.plantMapper.selectAll(plantQueryObject);
            for(Plant plant :listData){
                this.redisTemplate.opsForValue().set("plant_"+plant.getCid(),JSONObject.toJSONString(plant));
            }
            return  new PageResult(listData,count,plantQueryObject.getCurrentPage(),plantQueryObject.getPageSize()) ;
        }


    }

    @Override
    public boolean updatePlantInstockRecordTime(int cid) {
        boolean flag = this.plantMapper.updateTimeByPrimaryKey(cid) ;
        if(flag){
            this.rabbitMQSender.send("data-change-queue","{\"event_type\": \"update\", \"data_type\": \"plant\", \"cid\": " + cid +   "}");
        }
        return  flag ;
    }
}
